//
//  WFAdViewController.m
//  WangyiFangzuo
//
//  Created by 瑜 on 16/9/12.
//  Copyright © 2016年 111. All rights reserved.
//

#import "WFAdViewController.h"
#import "WFtabarViewController.h"
#import <AFNetworking.h>
#import "WFModel.h"
#import <UIImageView+WebCache.h>
@interface WFAdViewController ()
@property (weak, nonatomic) IBOutlet UIButton *btn;
- (IBAction)btnTiaoZhuan:(id)sender;
@property (weak, nonatomic) IBOutlet UIImageView *Adimage;
@property(nonatomic,strong)NSTimer *timer;
@property(nonatomic,strong)NSMutableArray *arrM;
@end

@implementation WFAdViewController

- (NSMutableArray *)arrM
{
    if(!_arrM)
    {
        _arrM = [NSMutableArray array];
    }
    return _arrM;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _btn.layer.cornerRadius = 15;
    _btn.clipsToBounds = YES;
    [self adDataLoad];
    _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(minutes) userInfo:nil repeats:YES];
}
//定时器方法
- (void)minutes
{
    static int count = 2;
//    NSString *title = [NSString stringWithFormat:@"%d",count];
//    [_btn setTitle:title forState:UIControlStateNormal];
    if(count == 0)
    {
        [UIApplication sharedApplication].keyWindow.rootViewController = [[WFtabarViewController alloc] init];
        [_timer invalidate];
    }
    count --;
}
//跳转方法
- (IBAction)btnTiaoZhuan:(id)sender
{
    [UIApplication sharedApplication].keyWindow.rootViewController = [[WFtabarViewController alloc] init];
    [_timer invalidate];
}
#pragma mark 广告数据请求
- (void)adDataLoad
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    [manager GET:@"http://g1.163.com/madr?app=7A16FBB6&platform=android&category=STARTUP&location=1&timestamp=1468218743467&uid=EZ3bZWDXmKdsK4nvCc35n0JkyjzYeO0CqopHlF8QI8etKshKRuUV3B6xar8t%2BkELe%2BD80Civ122M6tHucNL66Los%2FAFtWvVFLYdFIyI4oQlnQ6z9ij4gvi7cNy%2FTDaR8S9aTVlCisKQUAXj3glvJhdtuJ0C2ar62bTOscoCt%2BzP5I6uYvXze9MVRQ7v6VchuQomcuy02S7kR88geW9IuOXNb1BN5zYJCTTDBvLTDR8vg7JzTg5h3URtHOnvWpNEOoy8FNVyV3f3VsPqugiVI7g%2B%2BW9S7AwdgZOlNSOwIj8buuEe0MYs4xBGXynWnopkEKUbMaXdG6Qjcf2UHI4c%2B9o%2BOhb3DYOuda3WvPWSqT44%3D" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        for(NSDictionary *dict in responseObject[@"ads"])
        {
            WFModel *model = [WFModel modelWithDict:dict];
            [self.arrM addObject:model];
        }
        int count = arc4random()%5;
        WFModel *model = self.arrM[count];
        NSURL *url = [NSURL URLWithString:model.res_url[0]];
        [_Adimage sd_setImageWithURL:url];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}
@end
